// compontnts/input/lt-input.js
import {formVerify,string2Date,date2String} from "../../../utils/util"
import moment from 'moment'
const defaultSettings = require('../../../config/defaultSettings')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    formData:{
      type:Object,
      value:{}
    },
    field:{
      type:Object,
      value:{}
    },
    betweenSign:{
      type:String,
      value:""
    },
   
  },
  lifetimes: {
    ready:function(){
    },
    attached: function() {
      // 在组件实例进入页面节点树时执行,初始化一些操作
      let minDate = new Date()
      minDate = minDate.setFullYear(minDate.getFullYear()-10)
      if(this.data.field['minDate']){
        minDate = this.data.field['minDate']
      }

      let maxDate = new Date()
      maxDate = maxDate.setFullYear(maxDate.getFullYear()+10)
      if(this.data.field['maxDate']){
        maxDate = this.data.field['maxDate']
      }
      let currentDate=new Date().getTime()
      if(this.data.formData[this.data.field['field']+this.data.betweenSign]){
        currentDate = this.data.formData[this.data.field['field']+this.data.betweenSign].getTime()
      }
      this.setData({minDate:minDate,maxDate:maxDate,currentDate:currentDate})
      if(this.data.field['clean']){
        this.setData({cancelButtonText:'清空'})
      }
      
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  observers:{
    'formData':function(formData){
      if(formData[this.data.field['field']+this.data.betweenSign]){
        this.setData({value:formData[this.data.field['field']+this.data.betweenSign]})
      }else{
        this.setData({value:""})
      }
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    errorMessage:"",
    isShow:false,
    primaryColor:defaultSettings.primaryColor,
    minDate:null,
    maxDate:null,
    currentDate:new Date(),
    value:'',
    cancelButtonText:'取消',
    moment,
    
  },
 
  /**
   * 组件的方法列表
   */
  methods: {
    
    onChange(event) {
  
    },
    showPicker(){
      if(this.data.field['disabled']){
        this.setData({ isShow: false });
      }else{
        this.setData({ isShow: !this.data.isShow });
      }
    },
    onClose(){
      this.setData({ isShow: false });
    },

    onBlur(e){
      this.verify()
    },
    verify(){
      return formVerify(this)
    },
    onConfirm(event) {
      this.setData({ isShow: false,value:date2String(event.detail,this.data.field['type']),currentDate:event.detail})
      //将输入框改变的值实时赋值给formData
      this.data.formData[this.data.field['field']+this.data.betweenSign]=this.data.value
      //调用父级，将值传出
      this.triggerEvent("change",{field:this.data.field['field']+this.data.betweenSign,val:this.data.value})
      this.verify()
    },
    onCancel(){
      this.setData({ isShow: false });
      if(this.data.field['clean']){
        this.setData({value:''})
        //将输入框改变的值实时赋值给formData
        this.data.formData[this.data.field['field']+this.data.betweenSign]=''
        //调用父级，将值传出
        this.triggerEvent("change",{field:this.data.field['field']+this.data.betweenSign,val:''})
      }
      this.verify()
    },
   
  }
})
